package datastructures;

public class Vector2f {
	
	float x;
	float y;
	
	public Vector2f(float x, float y)
	{
		this.x = x;
		this.y = y;
	}
	
	public Vector2f(float xy)
	{
		this.x = xy;
		this.y = xy;
	}
	
	public Vector2f(Vector2f vec)
	{
		this.x = vec.x;
		this.y = vec.y;
	}
	
	public synchronized float x()
	{
		return this.x;
	}
	
	public synchronized float y()
	{
		return this.y;
	}
	
	public synchronized void invert()
	{
		this.x = -this.x;
		this.y = -this.y;
	}
	
	public synchronized void add(Vector2f vec)
	{
		this.x += vec.x;
		this.y += vec.y;
	}
	
	public synchronized void normalize()
	{
		float length = this.length();
		this.x /= length;
		this.y /= length;
	}
	
	public synchronized void scale(float scale)
	{
		this.x *= scale;
		this.y *= scale;
	}
	
	public synchronized Vector2f scale_cpy(float scale)
	{
		return new Vector2f(this.x * scale, this.y * scale);
	}
	
	public synchronized float length()
	{
		return (float) Math.sqrt(x * x + y * y);
	}
	
	public synchronized boolean isInRange(Vector2f vec, float range)
	{
		return (subtract(this,vec).length()<range);
	}
	
	@Override
	public String toString()
	{
		return "("+x+"|"+y+")";
		
	}	
	
	@Override
	public synchronized boolean equals(Object obj) {
		return (this.x == ((Vector2f)obj).x && this.y == ((Vector2f)obj).y);
	}
	
	public static Vector2f add(Vector2f vec1, Vector2f vec2)
	{
		return new Vector2f(vec1.x + vec2.x, vec1.y + vec2.y);
	}
	
	public static Vector2f subtract(Vector2f vec1, Vector2f vec2)
	{
		return new Vector2f(vec1.x - vec2.x, vec1.y - vec2.y);
	}
	
}
